package ru.linnenson.darshana.infrastructure.persistence;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import ru.linnenson.darshana.application.repositories.TaskRepository;
import ru.linnenson.darshana.infrastructure.persistence.models.LevelStore;
import ru.linnenson.darshana.infrastructure.persistence.models.MathTaskStore;
import ru.linnenson.darshana.infrastructure.persistence.models.NumberInKimStore;
import ru.linnenson.darshana.infrastructure.persistence.models.PhysicTaskStore;
import ru.linnenson.darshana.infrastructure.persistence.models.SourceStore;
import ru.linnenson.darshana.infrastructure.persistence.models.ThemeStore;
import ru.linnenson.darshana.infrastructure.persistence.repositories.LevelPersistenceRepository;
import ru.linnenson.darshana.infrastructure.persistence.repositories.MathTaskPersistenceRepository;
import ru.linnenson.darshana.infrastructure.persistence.repositories.NumberInKimPersistenceRepository;
import ru.linnenson.darshana.infrastructure.persistence.repositories.PhysicTaskPersistenceRepository;
import ru.linnenson.darshana.infrastructure.persistence.repositories.SourcePersistenceRepository;
import ru.linnenson.darshana.infrastructure.persistence.repositories.ThemePersistenceRepository;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class TaskRepositoryImpl implements TaskRepository {

  private final LevelPersistenceRepository levelPersistenceRepository;
  private final ThemePersistenceRepository themePersistenceRepository;
  private final SourcePersistenceRepository sourcePersistenceRepository;
  private final MathTaskPersistenceRepository mathTaskPersistenceRepository;
  private final PhysicTaskPersistenceRepository physicTaskPersistenceRepository;
  private final NumberInKimPersistenceRepository numberInKimPersistenceRepository;

  @Override
  public Integer createPhysic(final Integer level, final Integer source, final Integer theme, final Integer numberInKim,
                              final List<String> tags) {

    final Optional<LevelStore> levelStore = levelPersistenceRepository.findById(level);
    final Optional<NumberInKimStore> numberInKimStore = numberInKimPersistenceRepository.findById(numberInKim);
    final Optional<SourceStore> sourceStore = sourcePersistenceRepository.findById(source);
    final Optional<ThemeStore> themeStore = themePersistenceRepository.findById(theme);

    if (levelStore.isEmpty() || numberInKimStore.isEmpty() || sourceStore.isEmpty() || themeStore.isEmpty()) {
      throw new EntityNotFoundException();
    }

    final PhysicTaskStore physicTaskStore = new PhysicTaskStore();

    physicTaskStore.setLevel(levelStore.get());
    physicTaskStore.setTheme(themeStore.get());
    physicTaskStore.setSource(sourceStore.get());
    physicTaskStore.setNumberInKim(numberInKimStore.get());
    physicTaskStore.setTags(tags);

    return physicTaskPersistenceRepository.save(physicTaskStore).getId();
  }

  @Override
  public Integer createMath(final Integer source, final Integer theme, final Integer numberInKim,
                            final List<String> tags) {

    final Optional<NumberInKimStore> numberInKimStore = numberInKimPersistenceRepository.findById(numberInKim);
    final Optional<SourceStore> sourceStore = sourcePersistenceRepository.findById(source);
    final Optional<ThemeStore> themeStore = themePersistenceRepository.findById(theme);

    if (numberInKimStore.isEmpty() || sourceStore.isEmpty() || themeStore.isEmpty()) {
      throw new EntityNotFoundException();
    }

    final MathTaskStore mathTaskStore = new MathTaskStore();

    mathTaskStore.setTheme(themeStore.get());
    mathTaskStore.setSource(sourceStore.get());
    mathTaskStore.setNumberInKim(numberInKimStore.get());
    mathTaskStore.setTags(tags);

    return mathTaskPersistenceRepository.save(mathTaskStore).getId();
  }
}
